package com.lucweb.dao.impl;


import com.lucweb.dao.WxtaskDao;
import com.lucweb.entity.*;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Repository("wxtaskDao")
public class WxtaskDaoImpl implements WxtaskDao {

    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;

    private JctaskIndex jctaskIndex;

    private TaskBean taskBean;



    @Override
    public List getAllwxTask() {
        //处理活的的检测任务列表、
        List<JctaskIndex> taskList = sessionFactory.getCurrentSession().createSQLQuery(
                "SELECT sys_jctask.task_id,sys_jctask.task_name,(SELECT count(question_id) FROM sys_question " +
                "WHERE question_id_task_id\n" +
                " = sys_jctask.task_id) AS question_count ,(SELECT count(question_id) FROM sys_question WHERE\n" +
                "question_id_task_id = sys_jctask.task_id AND question_solve = \"1\" )\n" +
                "AS question_solve,(SELECT count(question_id) FROM sys_question WHERE\n" +
                "question_id_task_id = sys_jctask.task_id  AND question_difficult = \"1\" ) AS  question_diff ,\n" +
                "task_time,task_people FROM sys_jctask")
                .addScalar("task_id",StandardBasicTypes.STRING)
                .addScalar("question_count", StandardBasicTypes.STRING)
                .addScalar("task_name",StandardBasicTypes.STRING)
                .addScalar("task_time",StandardBasicTypes.STRING)
                .addScalar("task_people",StandardBasicTypes.STRING)
                .addScalar("question_solve",StandardBasicTypes.STRING)
                .addScalar("question_diff",StandardBasicTypes.STRING)
                .setResultTransformer(Transformers.aliasToBean(JctaskIndex.class)).list();

        return taskList;
    }

    @Override
    public List<TaskWx> wxTask(String para) {
//        Jctask jctask = (Jctask) sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM sys_jctask WHERE task_id = '1'")
//                .setResultTransformer(Transformers.aliasToBean(Jctask.class));


        List<TaskWx> taskWxList = (List<TaskWx>) sessionFactory.getCurrentSession().createQuery("FROM TaskWx where "+para ).list();



        return taskWxList;
    }

    @Override
    public String wxTaskNumber() {
        String number = (String) sessionFactory.getCurrentSession().createSQLQuery("SELECT max(taskNumber) as max FROM task_wx")
                .addScalar("max",StandardBasicTypes.STRING).uniqueResult();


        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        if (number == null) {
            number = formatter.format(date) + "2001";
        } else {
            DecimalFormat df = new DecimalFormat("0000");
            number = formatter.format(date)
                    + df.format(1 + Integer.parseInt(number.substring(8, 12)));

        }


        return number;
    }

    @Override
    public void saveOrUpdate(TaskWx taskWx) {
         sessionFactory.getCurrentSession().saveOrUpdate(taskWx);
    }

    @Override
    public TaskWx getWxTaskById(int taskId) {
        return (TaskWx)sessionFactory.getCurrentSession().get(TaskWx.class, taskId);

    }

    @Override
    public void deleteWxtask(int taskId) {
        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskWx.class, taskId)
        );
    }


}
